package cc.rengu.igas.channel.alipay.biz.impl;

import cc.rengu.igas.channel.alipay.common.constant.AlipayServiceConstant;
import cc.rengu.igas.channel.alipay.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.alipay.facade.AlipayMchntService;
import cc.rengu.igas.channel.alipay.facade.request.*;
import cc.rengu.igas.channel.alipay.facade.response.*;
import cc.rengu.igas.channel.alipay.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

public class AlipayMchntServiceImpl implements AlipayMchntService {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public Result<AliCreateMerchantResponse> aliCreateMerchant(AliCreateMerchantRequest aliCreateMerchantRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<AliCreateMerchantResponse> result = new Result<>();
        AliCreateMerchantResponse createMerchantResponse = new AliCreateMerchantResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(aliCreateMerchantRequest,
                    AlipayServiceConstant.CREATE_MCHNT_SERVICE, AliCreateMerchantResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((AliCreateMerchantResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("商户入驻失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                createMerchantResponse.setRespDesc(((BizException) e).getCode());
                createMerchantResponse.setRespDesc(e.getMessage());
            } else {
                createMerchantResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                createMerchantResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<AliQueryMerchantResponse> aliQueryMerchant(AliQueryMerchantRequest aliQueryMerchantRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<AliQueryMerchantResponse> result = new Result<>();
        AliQueryMerchantResponse queryMerchantResponse = new AliQueryMerchantResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(aliQueryMerchantRequest,
                    AlipayServiceConstant.QUERY_MCHNT_SERVICE, AliQueryMerchantResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((AliQueryMerchantResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("查询商户信息失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                queryMerchantResponse.setRespDesc(((BizException) e).getCode());
                queryMerchantResponse.setRespDesc(e.getMessage());
            } else {
                queryMerchantResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                queryMerchantResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<AliUpdateMerchantResponse> aliUpdateMerchant(AliUpdateMerchantRequest aliUpdateMerchantRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<AliUpdateMerchantResponse> result = new Result<>();
        AliUpdateMerchantResponse updateMerchantResponse = new AliUpdateMerchantResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(aliUpdateMerchantRequest,
                    AlipayServiceConstant.UPDATE_MCHNT_SERVICE, AliUpdateMerchantResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((AliUpdateMerchantResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("更新商户信息失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                updateMerchantResponse.setRespDesc(((BizException) e).getCode());
                updateMerchantResponse.setRespDesc(e.getMessage());
            } else {
                updateMerchantResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                updateMerchantResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<AliRiskMchntQueryResponse> aliRiskQuery(AliRiskMchntQueryRequest aliRiskMchntQueryRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<AliRiskMchntQueryResponse> result = new Result<>();
        AliRiskMchntQueryResponse riskMchntQueryResponse = new AliRiskMchntQueryResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(aliRiskMchntQueryRequest,
                    AlipayServiceConstant.RISK_MCHNT_QUERY_SERVICE, AliRiskMchntQueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((AliRiskMchntQueryResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("风险商户信息查询失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                riskMchntQueryResponse.setRespDesc(((BizException) e).getCode());
                riskMchntQueryResponse.setRespDesc(e.getMessage());
            } else {
                riskMchntQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                riskMchntQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<ActivityCreateResponse> activityCreate(ActivityCreateRequest activityCreateRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<ActivityCreateResponse> result = new Result<>();
        ActivityCreateResponse activityCreateResponse = new ActivityCreateResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(activityCreateRequest,
                    AlipayServiceConstant.ACTIVITY_CREATE_SERVICE, ActivityCreateResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((ActivityCreateResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("间连商户运营活动报名失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                activityCreateResponse.setRespDesc(((BizException) e).getCode());
                activityCreateResponse.setRespDesc(e.getMessage());
            } else {
                activityCreateResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                activityCreateResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<ActivityConfirmResponse> activityConfirm(ActivityConfirmRequest activityConfirmRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<ActivityConfirmResponse> result = new Result<>();
        ActivityConfirmResponse activityConfirmResponse = new ActivityConfirmResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(activityConfirmRequest,
                    AlipayServiceConstant.ACTIVITY_CONFIRM_SERVICE, ActivityConfirmResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((ActivityConfirmResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("商户运营活动确认失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                activityConfirmResponse.setRespDesc(((BizException) e).getCode());
                activityConfirmResponse.setRespDesc(e.getMessage());
            } else {
                activityConfirmResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                activityConfirmResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<AliGetAppAuthCodeResponse> getAppAuthCode(AliGetAppAuthCodeRequest aliGetAppAuthCodeRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<AliGetAppAuthCodeResponse> result = new Result<>();
        AliGetAppAuthCodeResponse aliGetAppAuthCodeResponse = new AliGetAppAuthCodeResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(aliGetAppAuthCodeRequest,
                    AlipayServiceConstant.ALI_GET_APP_AUTH_CODE_SERVICE, AliGetAppAuthCodeResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((AliGetAppAuthCodeResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("获取商户应用授权URL失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                aliGetAppAuthCodeResponse.setRespDesc(((BizException) e).getCode());
                aliGetAppAuthCodeResponse.setRespDesc(e.getMessage());
            } else {
                aliGetAppAuthCodeResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                aliGetAppAuthCodeResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<AliGetAppAuthTokenResponse> getAppAuthToken(AliGetAppAuthTokenRequest aliGetAppAuthTokenRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<AliGetAppAuthTokenResponse> result = new Result<>();
        AliGetAppAuthTokenResponse aliGetAppAuthTokenResponse = new AliGetAppAuthTokenResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(aliGetAppAuthTokenRequest,
                    AlipayServiceConstant.ALI_GET_APP_AUTH_TOKEN_SERVICE, AliGetAppAuthTokenResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((AliGetAppAuthTokenResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("获取商户应用授权TOKEN失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                aliGetAppAuthTokenResponse.setRespDesc(((BizException) e).getCode());
                aliGetAppAuthTokenResponse.setRespDesc(e.getMessage());
            } else {
                aliGetAppAuthTokenResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                aliGetAppAuthTokenResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<AliAcquireTermInfoResponse> aliAcquireTermInfo(AliAcquireTermInfoRequest aliAcquireTermInfoRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<AliAcquireTermInfoResponse> result = new Result<>();
        AliAcquireTermInfoResponse aliAcquireTermInfoResponse = new AliAcquireTermInfoResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(aliAcquireTermInfoRequest,
                    AlipayServiceConstant.ACQUIRE_TERM_INFO_SERVICE, AliAcquireTermInfoResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((AliAcquireTermInfoResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("终端信息采集失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                aliAcquireTermInfoResponse.setRespDesc(((BizException) e).getCode());
                aliAcquireTermInfoResponse.setRespDesc(e.getMessage());
            } else {
                aliAcquireTermInfoResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                aliAcquireTermInfoResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }
}
